﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpGL;
using SharpGL.SceneGraph.Assets;
namespace bitzhuwei.FeedingFrenzy.WorldModel
{
    public class Utility
    {
        private static float rotation;
        private static float rtri;
        //public static void DrawBall(OpenGL gl, TriTuple<double> center, double radius)
        //{
        //    gl.LoadIdentity();
        //    gl.Translate(center.X, center.Y, center.Z);
        //    gl.Begin(OpenGL.GL_TRIANGLE_STRIP);
        //    double theta = 0, phir = 0, phi = 0, thetar = 0, phir20 = 0;
        //    double x = 0, y = 0, z = 0;
        //    double slice = 10;
        //    var c = Math.PI / 180;
        //    for (phi = -90.0; phi <= 90.0; phi += slice)
        //    {
        //        phir = c * phi;
        //        phir20 = c * (phi + slice);
        //        for (theta = -180.0; theta <= 180.0; theta += slice)
        //        {
        //            thetar = c * theta;
        //            x = radius * Math.Sin(thetar) * Math.Cos(phir);
        //            y = radius * Math.Cos(thetar) * Math.Cos(phir);
        //            z = radius * Math.Sin(phir);
        //            gl.Vertex(x, y, z);
        //            x = radius * Math.Sin(thetar) * Math.Cos(phir20);
        //            y = radius * Math.Cos(thetar) * Math.Cos(phir20);
        //            z = radius * Math.Sin(phir20);
        //            gl.Vertex(x, y, z);
        //        }
        //        gl.End();
        //        //gl.PolygonMode(OpenGL.GL_FRONT, OpenGL.GL_LINE);
        //        //gl.PolygonMode(OpenGL.GL_BACK, OpenGL.GL_LINE);
        //        //gl.FrontFace(OpenGL.GL_CCW);
        //        gl.Begin(OpenGL.GL_TRIANGLE_STRIP);
        //        gl.Vertex(0.0, 0.0, radius);
        //        double c80 = c * 90.0;
        //        z = radius * Math.Sin(c80);
        //        for (theta = -180.0; theta <= 180.0; theta += slice)
        //        {
        //            thetar = c * theta;
        //            x = radius * Math.Sin(thetar) * Math.Cos(c80);
        //            y = radius * Math.Cos(thetar) * Math.Cos(c80);
        //            gl.Vertex(x, y, z);
        //        }
        //        gl.End();
        //        //gl.PolygonMode(OpenGL.GL_FRONT, OpenGL.GL_LINE);
        //        //gl.PolygonMode(OpenGL.GL_BACK, OpenGL.GL_LINE);
        //        //gl.FrontFace(OpenGL.GL_CCW);
        //        gl.Begin(OpenGL.GL_TRIANGLE_STRIP);
        //        gl.Vertex(0.0, 0.0, -radius);
        //        z = -radius * Math.Sin(c80);
        //        for (theta = -180.0; theta <= 180.0; theta += slice)
        //        {
        //            thetar = c * theta;
        //            x = radius * Math.Sin(thetar) * Math.Cos(c80);
        //            y = radius * Math.Cos(thetar) * Math.Cos(c80);
        //            gl.Vertex(x, y, z);
        //        }
        //        gl.End();
        //    }
        //}


        public static void SetCube(OpenGL gl)
        {
            if (texture != null)
                texture.Destroy(gl);
            texture = new Texture();
            texture.Create(gl, "Crate.bmp");
        }

        static Texture texture = null;
        public static void DrawCube(OpenGL gl, TriTuple<double> position)
        {
            gl.LoadIdentity();
            gl.Translate(position.X, position.Y, position.Z);
            var scaleMode = 0.2;
            gl.Scale(scaleMode, scaleMode, scaleMode);

            gl.Rotate(rtri, 0.0f, 1.0f, 0.0f);

            if (texture != null)
            {
                //  Bind the texture.
                texture.Bind(gl);
            }

            gl.Begin(OpenGL.GL_QUADS);

            // Front Face
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);	// Bottom Left Of The Texture and Quad
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);	// Bottom Right Of The Texture and Quad
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);	// Top Right Of The Texture and Quad
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);	// Top Left Of The Texture and Quad

            // Back Face
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);	// Top Right Of The Texture and Quad
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);	// Top Left Of The Texture and Quad
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad

            // Top Face
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);	// Top Left Of The Texture and Quad
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);	// Bottom Left Of The Texture and Quad
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, 1.0f, 1.0f);	// Bottom Right Of The Texture and Quad
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);	// Top Right Of The Texture and Quad

            // Bottom Face
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);	// Top Right Of The Texture and Quad
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, -1.0f, -1.0f);	// Top Left Of The Texture and Quad
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);	// Bottom Left Of The Texture and Quad
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);	// Bottom Right Of The Texture and Quad

            // Right face
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(1.0f, -1.0f, -1.0f);	// Bottom Right Of The Texture and Quad
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(1.0f, 1.0f, -1.0f);	// Top Right Of The Texture and Quad
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(1.0f, 1.0f, 1.0f);	// Top Left Of The Texture and Quad
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(1.0f, -1.0f, 1.0f);	// Bottom Left Of The Texture and Quad

            // Left Face
            gl.TexCoord(0.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, -1.0f);	// Bottom Left Of The Texture and Quad
            gl.TexCoord(1.0f, 0.0f); gl.Vertex(-1.0f, -1.0f, 1.0f);	// Bottom Right Of The Texture and Quad
            gl.TexCoord(1.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, 1.0f);	// Top Right Of The Texture and Quad
            gl.TexCoord(0.0f, 1.0f); gl.Vertex(-1.0f, 1.0f, -1.0f);	// Top Left Of The Texture and Quad
            gl.End();

            gl.Flush();

            rtri += 10.0f;// 0.2f;						// Increase The Rotation Variable For The Triangle 
        }
   
        public static void DrawPyramid(OpenGL gl, TriTuple<double> position)
        {
            gl.LoadIdentity();
            gl.Translate(position.X, position.Y, position.Z);

            //  Rotate around the Y axis.
            gl.Rotate(rotation, 0.0f, 1.0f, 0.0f);
            
            //  Draw a coloured pyramid.
            gl.Begin(OpenGL.GL_TRIANGLES);
            gl.Color(1.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 1.0f, 0.0f);
            gl.Color(0.0f, 1.0f, 0.0f);
            gl.Vertex(-1.0f, -1.0f, 1.0f);
            gl.Color(0.0f, 0.0f, 1.0f);
            gl.Vertex(1.0f, -1.0f, 1.0f);
            gl.Color(1.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 1.0f, 0.0f);
            gl.Color(0.0f, 0.0f, 1.0f);
            gl.Vertex(1.0f, -1.0f, 1.0f);
            gl.Color(0.0f, 1.0f, 0.0f);
            gl.Vertex(1.0f, -1.0f, -1.0f);
            gl.Color(1.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 1.0f, 0.0f);
            gl.Color(0.0f, 1.0f, 0.0f);
            gl.Vertex(1.0f, -1.0f, -1.0f);
            gl.Color(0.0f, 0.0f, 1.0f);
            gl.Vertex(-1.0f, -1.0f, -1.0f);
            gl.Color(1.0f, 0.0f, 0.0f);
            gl.Vertex(0.0f, 1.0f, 0.0f);
            gl.Color(0.0f, 0.0f, 1.0f);
            gl.Vertex(-1.0f, -1.0f, -1.0f);
            gl.Color(0.0f, 1.0f, 0.0f);
            gl.Vertex(-1.0f, -1.0f, 1.0f);
            gl.End();

            //  Nudge the rotation.
            rotation += 3.0f;
        }
    }
}